<?php
/**
 * Created by zhangyh.
 * User: zhangyh
 * Date: 2018/11/3
 * Time: 16:35
 */

namespace App\Service;


class BaseService
{

    public $errCode = '0';
    public $errMsg = '';
    public $data = [];


    public $model;
    public $perPage = 15;
    public $primaryKey = 'id';


    public function __construct($modelName = null)
    {
        if (!empty($modelName)) {
            $m = '\\App\\Model\\' . $modelName;
            $this->model = new $m();
        }
    }


    /**
     * 设置参数
     * @param $data
     * @return $this
     * @author 张永辉 2018年11月3日
     */
    public function setParams($data)
    {
        foreach ($data as $key => $val) {
            $this->$key = $val;
        }
        return $this;
    }

    /**
     * 获取返回信息
     * @return array
     * @author 张永辉 2018年11月3日
     */
    public function getReturn()
    {
        return [
            'errCode' => $this->errCode,
            'errMsg'  => $this->errMsg,
            'data'    => $this->data,
        ];
    }  

    /**
     * 获取返回信息
     * @return array
     * @author 张永辉 2018年11月5日
     */
    public function dealKey($data,$pid='id')
    {
        $result = [];
        foreach ($data as $val){
            $result[$val[$pid]] = $val;
        }
        return $result;
    }

    public function count($where = [])
    {
        return $this->model->where($where)->count();
    }

    /**
     * 预加载关联关系
     * @param  array  $withAll [对应的关联关系数据]
     * @return [type]          [description]
     */
    public function withAll($withAll=[])
    {
        if (!empty($withAll)) {
            $this->model->with = $withAll;
        }
        return $this;
    }

    /**
     * 分页获取活动列表数据
     * @param  array   $where    [数据条件]
     * @param  string  $orderBy  [排序字段]
     * @param  string  $order    [排序类型]
     * @param  boolean $is_page  [是否分页]
     * @param  integer $pageSize [每页显示的数量]
     * @return [obj]            [模型列表实例]
     */
    public function getAllList($where = [],$orderBy='created_at',$order='desc',$is_page=true,$pageSize = 10)
    {
        $select = $this->model;

        if ($this->model->with) {
            $select = $select->with($this->model->with);
        }
        if ($where) {
            $select = $select->where($where);
        }
        $select = $select->orderBy($orderBy,$order);

        if ($is_page) {
            $list = $select->paginate($pageSize);
        }else {
            $list= $select->all();
        }
        return $list;
    }

    /**
     * 根据id获取相应单条记录数据
     * @param  [integer] $id [应对ID]
     * @return [obj]     [对应模型实例]
     */
    public function getRow($id)
    {
        $select = $this->model;
        if ($this->model->with) {
            $select = $select->with($this->model->with);
        }
        $data = $select->find($id);

        return $data;
    }

    /**
     * 根据条件获取相应单条记录数据
     * @param  [array] $where [条件数组]
     * @return [obj]     [对应模型实例]
     */
    public function getRowByCondition($where)
    {
        $select = $this->model;
        if ($this->model->with) {
            $select = $select->with($this->model->with);
        }
        $data = $select->where($where)->first();

        return $data;
    }

    /**
     * 添加数据
     * @param [type] $data [要添加的数据]
     * @return obj [模型实例]
     */
    public function add($data)
    {
        return $this->model->create($data);
    }

    /**
     * 更新单条记录
     * @param  [integer] $id   [对应id]
     * @param  [array] $data [需要更新的数据]
     * @return [obj]       [实例模型]
     */
    public function saveRow($id,$data)
    {
        $select = $this->model->find($id);
        $select->fill($data);
        $select->save();
        return $this->model;
    }

    /**
     * 批量更新数据
     * @param  [array] $where [条件数组]
     * @param  [array] $data  [需要更新的数据]
     * @return [integer]        [返回更新的数量]
     */
    public function updateBatch($where,$data)
    {
        return $this->model->where($where)->update($data);
    }



    public function del($id)
    {
        if ($select = $this->model->find($id)) {
            return $select->delete();
        }
        return true;
    }

    /**
     * 批量删除数据
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function deleteBatch($where)
    {
        return $this->where($where)->delete();
    }

}